package com.closer.server.biz.images.validator;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.IoUtil;
import com.closer.server.biz.images.dto.SearchRequest;
import com.closer.server.biz.images.entity.Images;
import com.closer.server.biz.images.enums.ImageTypeEnums;
import com.closer.server.mapper.image.ImagesMapper;
import com.closer.server.support.exception.BusinessException;
import com.closer.server.support.exception.ErrorCode;
import com.closer.server.support.exception.ThrowUtils;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import static cn.hutool.core.lang.Console.log;

@Component
public class ImagesValidator {

    private final ImagesMapper mapper;

    public ImagesValidator(ImagesMapper mapper){
        this.mapper = mapper;
    }

    /**
     *  图片上传校验
     * @param type 图片类型
     * @param file 图片输入流
     * @return 图片
     */
    public File validate(String type, InputStream file){
        ThrowUtils.throwIf(type == null || file == null, ErrorCode.PARAMS_ERROR,"参数不正确");
        ThrowUtils.throwIf(ImageTypeEnums.getEnumByValue(type) == null, ErrorCode.PARAMS_ERROR,"图片类型不正确");

        var fileBytes = IoUtil.readBytes(file);
        final int MAX_SIZE = 50 * 1024;
        ThrowUtils.throwIf(fileBytes.length > MAX_SIZE, ErrorCode.PARAMS_ERROR,"图片大小不能超过50KB，当前大小：" + (fileBytes.length / 1024) + "KB");
        var image = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
        try {
            image = ImgUtil.read(IoUtil.toStream(fileBytes));
            log(image);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"图片不存在");
        }
        var width = image.getWidth();
        var height = image.getHeight();
        if (type.equals(ImageTypeEnums.CAPTCHA.getValue()) &&(width !=600)&& (height != 360)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "图片大小不正确");
        }
        try {
            var tempFile = File.createTempFile("image-", ".jpg");
            var jpgImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            var g2d = jpgImage.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
            g2d.drawImage(image, 0, 0, null);
            g2d.dispose();
            ImgUtil.write(jpgImage, tempFile);
            return tempFile;
        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "图片转换失败");
        }
    }

    /**
     * 验证图片是否存在
     * @param id 题目ID
     */
    public Images validateExitImage(Long id){
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR, "图片ID不能为空");
        ThrowUtils.throwIf(id<=0, ErrorCode.PARAMS_ERROR, "图片ID不能小于0");
        var img = mapper.selectOneById(id);
        ThrowUtils.throwIf(img == null, ErrorCode.PARAMS_ERROR, "图片不存在");
        return img;
    }

    /**
     * 搜索图片参数校验
     * @param params 参数
     */
    public void validate(SearchRequest params) {
        if (params.getId() != null) {
            ThrowUtils.throwIf(params.getId() <= 0, ErrorCode.PARAMS_ERROR,"图片Id错误");
        }
        if (params.getType() != null) {
            ThrowUtils.throwIf(ImageTypeEnums.getEnumByValue(params.getType()) == null, ErrorCode.PARAMS_ERROR,"图片类型不正确");
        }
        // enabled 为 Boolean，可为空或 true/false，无需其他校验
    }
}
